package com.shujia.day14;

public class Demo06DealLock {
    static final MyLock LOCK1 = new MyLock();
    static final MyLock LOCK2 = new MyLock();

    public static void main(String[] args) {
        /*
            TODO 线程死锁
                 两个线程之间相互依赖，程序无法继续执行
         */
        DealRunnable dealRunnable = new DealRunnable();
        new Thread(dealRunnable,"线程1").start();
        new Thread(dealRunnable,"线程2").start();

    }

    static class MyLock {

    }

    static class DealRunnable implements Runnable {

        @Override
        public void run() {
            Thread currentThread = Thread.currentThread();
            if (currentThread.getName() == "线程1"){
                synchronized (LOCK1){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("线程1已经进入 synchronized (LOCK1)");
                    synchronized (LOCK2){
                        System.out.println("线程1执行完成....");
                    }

                }
            }else {
                synchronized (LOCK2){
//                    try {
//                        Thread.sleep(100);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
                    System.out.println("线程2已经进入 synchronized (LOCK2)");
                    synchronized (LOCK1){
                        System.out.println("线程2执行完成....");
                    }

                }
            }



        }
    }
}
